Extension::JsonPathToOffset Method

Syntax

.PathToOffset as n (json as C, path as C [, base as N])

Arguments

json

Json input text

path

Path to get starting offset of.

base

Optional array/position base.

Description

Get the offset in the json (1 based) - for the 'path'. The 'base' argument indicates array/position start - Xbasic should be 1, javascript should be 0.

Discussion

This method can be used to divide or decorate an element in json string, usually this would be used in conjuction with PathExtract so that the length of the element can also be determined.

Example

dim json as c = <<%json%
{
    "fname": "john",
    "lname": "public",
    "address": {
        "address1": "12 main street",
        "address2": "apt 2b",
        "state": "NY",
        "city": "Springfield"
    },
    "bill_address": {
        "address1": "12 main street",
        "address2": "apt 2b",
        "state": "NY",
        "city": "Statesville"
    }
}
%json%

pos = extension::json::PathToOffset(json,"address.state")
? pos
= 155

before = left(json,pos-1)
after = substr(json,pos)
? before+"Contents for address.state here>>"+after
= {
    "fname": "john",
    "lname": "public",
    "address": {
        "address1": "12 main street",
        "address2": "apt 2b",
        "state": Contents for address.state here>>"NY",
        "city": "Springfield"
    },
    "bill_address": {
        "address1": "12 main street",
        "address2": "apt 2b",
        "state": "NY",
        "city": "Statesville"
    }
}